<template>
  <div class="work-calendar-container">
    <el-row :gutter="10" type="flex" justify="end">
      <el-col :span="6">
        <el-select v-model="currentYear" size="small" placeholder="选择年份" @change="changeDate">
          <el-option
            v-for="item in yearList"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-select v-model="currentMonth" size="small" placeholder="选择月份" @change="changeDate">
          <el-option
            v-for="item in 12"
            :key="item"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <!-- 日历控件 -->
    <el-calendar v-model="currentDate">
      <template #dateCell="{ date, data }">
        <div class="date-box">
          <span>{{ data.day | getDay }}</span>
          <el-button v-if="showFn(date)" type="warning" circle size="mini"
            >休</el-button
          >
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentYear: '', // 选择年份
      currentMonth: '', // 选择月份
      currentDate: 1663492059000, // 日历当前日期
      yearList: [] // 年份列表数据
    }
  },
  props: {
    // 定义一个开始日期的属性，可以从外部传入一个起始时间
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  filters: {
    getDay(day) {
      // day:2022-09-01
      // [2022, 09, 01]
      const dayStr = day.split('-')[2]
      if (dayStr.startsWith('0')) return dayStr.split('')[1]
      return dayStr
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 监听年份和日期的改变
    changeDate() {
      const year = this.currentYear
      const month = this.currentMonth
      this.currentDate = `${year}-${month}-1`
    },
    // 初始化时间
    initData() {
      this.currentYear = this.startDate.getFullYear()
      this.currentMonth = this.startDate.getMonth() + 1
      console.log(this.currentYear)
      console.log(Array(11))
      // Array.from(参数1， 参数2)
      /**
       * 参数1：接收一个数组，遍历数据触发参数2回调函数
       * 参数2：回调函数，作用类似于map对遍历的对象处理后返回一个新的数组
       */
      this.yearList = Array.from(Array(11), (value, index) => {
        console.log(value, index)
        return this.currentYear + index - 5
      })
      console.log(this.yearList)
    },
    showFn(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style lang="scss">
.work-calendar-container {
  .el-calendar__header {
    display: none;
  }
  .date-box {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .el-button--mini.is-circle {
      padding: 2px;
      margin-left: 5px;
    }
  }
  .el-calendar-table__row td,
  .el-calendar-table tr td:first-child,
  .el-calendar-table__row td.prev {
    border: none;
  }
}
</style>